package com.iasku.web;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.kobjects.base64.Base64;
import org.xmlpull.v1.XmlPullParser;

import com.iasku.constant.Constant;
import com.iasku.entity.IaskuCourse;
import com.iasku.entity.IaskuQuestion;
import com.iasku.util.XmlParserUtil;

public class XmlParser {

    public static void getQuestionById(String result, IaskuQuestion iQuestion) {
        XmlPullParser parser = XmlParserUtil.getXmlPullParser(result);
        try{
            int eventType = parser.getEventType();
            int choice_num = 0;
            String ans = "";
            while (eventType != XmlPullParser.END_DOCUMENT) {
                if (eventType == XmlPullParser.START_TAG) {
                    if ("field".equals(parser.getName())) {
                         if ("答案数".equals(parser.getAttributeValue(0))) {
                            iQuestion.setAnswerCount(parser.nextText());
                        } else if ("选项A".equals(parser.getAttributeValue(0))) {
                            choice_num++;
                        } else if ("选项B".equals(parser.getAttributeValue(0))) {
                            choice_num++;
                        } else if ("选项C".equals(parser.getAttributeValue(0))) {
                            choice_num++;
                        } else if ("选项D".equals(parser.getAttributeValue(0))) {
                            choice_num++;
                        } else if ("答案1".equals(parser.getAttributeValue(0))) {
                            ans += new String(Base64.decode(parser.nextText()));
                        } else if ("答案2".equals(parser.getAttributeValue(0))) {
                            ans += new String(Base64.decode(parser.nextText()));
                        } else if ("答案3".equals(parser.getAttributeValue(0))) {
                            ans += new String(Base64.decode(parser.nextText()));
                        } else if ("答案4".equals(parser.getAttributeValue(0))) {
                            ans += new String(Base64.decode(parser.nextText()));
                        } 
                    }
                }
                eventType = parser.next();
            }
            int qTypeid = iQuestion.getQuestion_type_id();
            if(qTypeid == Constant.TYPE_DANX || qTypeid == Constant.TYPE_DUOX){
                if(choice_num == 0){
                    choice_num = 4;
                }
            }else{
                choice_num = 0;
            }
            iQuestion.setChoicenum(choice_num);
            iQuestion.setTrueanswer(ans);
        }catch(Exception e){
            e.printStackTrace();
        }
    }

    public static Map<String, String> getVideoByCourseid(String result) {
        XmlPullParser parser = XmlParserUtil.getXmlPullParser(result);
        Map<String,String> map = new HashMap<String, String>();
        try{
            int eventType = parser.getEventType();
            while (eventType != XmlPullParser.END_DOCUMENT) {
                if (eventType == XmlPullParser.START_TAG) {
                    if ("field".equals(parser.getName())) {
                        if ("filename".equals(parser.getAttributeValue(0))) {
                            map.put("filename",parser.nextText());
                        }else if("filesize".equals(parser.getAttributeValue(0))){
                            map.put("filesize", parser.nextText());
                        }
                       
                    }
                }
                eventType = parser.next();
            }
        }catch(Exception e){
            e.printStackTrace();
        }
        
        return map;
    }

    public static void getCoursorByClassId(List<IaskuCourse> courses, String value) {
        XmlPullParser parser = XmlParserUtil.getXmlPullParser(value);
        IaskuCourse ic = null;
        if (parser != null) {
            try {
                int eventType = parser.getEventType();
                while (eventType != XmlPullParser.END_DOCUMENT) {
                    if (eventType == XmlPullParser.START_TAG) {
                        if ("record".equals(parser.getName())) {
                            ic = new IaskuCourse();
                        } else if ("field".endsWith(parser.getName())) {
                            String str = parser.getAttributeValue(0);
                            // System.out.println(str);
                            if ("Courseid".equals(str)) {
                                ic.setCourseid(parser.nextText());
                            } else if ("Coursename".equals(str)) {
                                ic.setCoursename(parser.nextText());
                            } else if ("subject".equals(str)) {
                                ic.setSubject(parser.nextText());
                            } else if ("teachid".equals(str)) {
                                ic.setTeacherid(parser.nextText());
                            } else if ("begintime".equals(str)) {
                                ic.setBegintime(parser.nextText());
                            } else if ("ispreview".equals(str)) {
                                ic.setIfpreview(parser.nextText());
                            } else if ("ifend".equals(str)) {
                                ic.setIfend(parser.nextText());
                            } else if ("classtype".equals(str)) {
                                ic.setClasstype(parser.nextText());
                            } else if ("endtime".equals(str)) {
                                ic.setEndtime(parser.nextText());
                            }
                        }
                    } else if (eventType == XmlPullParser.END_TAG) {
                        if ("record".equals(parser.getName())) {
                            String[] state = new String[2];
                            state[0] = "0";
                            state[1] = "0";
                            if ("true".equalsIgnoreCase(ic.getIfpreview())) {
                                state[0] = "1";
                            }
                            if ("true".equalsIgnoreCase(ic.getIfend())) {
                                state[1] = "1";
                            }
                            ic.setClassstate(state[0] + state[1]);
                            courses.add(ic);
                        }
                    }
                    eventType = parser.next();
                }
            } catch (Exception e) {
                // TODO: handle exception
            }
        }

    }
    

}
